package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.EncipherAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivityEncipherVideoBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class EncipherVideoActivity extends BaseAc<ActivityEncipherVideoBinding> {

    List<MyVideoBean> listSp = new ArrayList<>();
    List<MyVideoBean> listSel = new ArrayList<>();
    private EncipherAdapter encipherAdapter;
    private boolean isEdit = false;
    private Dialog myDeleteDialog;



    @Override
    protected int onCreate() {
        return R.layout.activity_encipher_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivEncipherVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivDeleteVideo.setOnClickListener(this);
        mDataBinding.tvManageVideo.setOnClickListener(this);
        mDataBinding.tvManageCancel.setOnClickListener(this);
        mDataBinding.rvEncipherVideoList.setLayoutManager(new GridLayoutManager(mContext, 2));
        encipherAdapter = new EncipherAdapter();
        mDataBinding.rvEncipherVideoList.setAdapter(encipherAdapter);
        encipherAdapter.setOnItemClickListener(this);
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvManageVideo:
                isEdit = true;
                encipherAdapter.setEdit(true);
                encipherAdapter.notifyDataSetChanged();
                mDataBinding.tvManageVideo.setVisibility(View.GONE);
                mDataBinding.tvManageCancel.setVisibility(View.VISIBLE);
                mDataBinding.llDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.tvManageCancel:
                cancelEdit();
                break;
            case R.id.ivDeleteVideo:
                if (listSel.size() == 0) return;
                myDeleteDialog.show();
                break;
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确定
                myDeleteDialog.dismiss();
                deleteVideo();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (encipherAdapter.getItem(position).isSelect()) {
                encipherAdapter.getItem(position).setSelect(false);
                listSel.remove(encipherAdapter.getItem(position));
            } else {
                encipherAdapter.getItem(position).setSelect(true);
                listSel.add(encipherAdapter.getItem(position));
            }
            encipherAdapter.notifyItemChanged(position);
        } else {
            PlayVideoActivity.videoUri = encipherAdapter.getItem(position).getPath();
            startActivity(PlayVideoActivity.class);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //删除视频
    private void deleteVideo() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (int i = 0; i < listSel.size(); i++) {
                            mContext.getContentResolver().delete(Uri.parse(listSel.get(i).getUrl()), null, null);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        cancelEdit();
                        getData();
                    }
                });
            }
        }, 500);
    }

    //取消编辑
    private void cancelEdit() {
        isEdit = false;
        encipherAdapter.setEdit(false);
        mDataBinding.tvManageVideo.setVisibility(View.VISIBLE);
        mDataBinding.tvManageCancel.setVisibility(View.GONE);
        mDataBinding.llDelete.setVisibility(View.GONE);
        listSel.clear();
        for (MyVideoBean bean : encipherAdapter.getValidData()) {
            bean.setSelect(false);
        }
        encipherAdapter.notifyDataSetChanged();
    }

    private void getData() {
        listSp.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.MyEnciper;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                Uri uri = UriUtil.file2Uri(mContext, file);
                listSp.add(new MyVideoBean(file.getName(), file.getPath(), uri.toString(), MediaUtil.getDuration(file.getPath()), FileUtils.getFileLastModified(file.getPath()), file.length(), false));
            }
        }
        if (listSp.size() > 0) {
            encipherAdapter.setList(listSp);
            mDataBinding.rvEncipherVideoList.setVisibility(View.VISIBLE);
            mDataBinding.ivEncipherVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvEncipherVideoList.setVisibility(View.GONE);
            mDataBinding.ivEncipherVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除提示
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView dialogConfirm = inflate.findViewById(R.id.ivDialogDeleteRight);
        dialogCancel.setOnClickListener(this);
        dialogConfirm.setOnClickListener(this);
    }

}